package org.jet.emall.reptile.utils;

import java.util.LinkedList;
import java.util.List;

public class StringUtils {
    public static List<String> getImagesFromCssText(String cssText) {
        List<String> images = new LinkedList<>();
        StringBuilder sb = new StringBuilder(cssText);
        String flag = "image:url(";
        while(true) {

            int begin = sb.indexOf(flag);
            if(begin<0) {
                break;
            }
            begin += flag.length();

            sb.delete(0, begin);
            int end = sb.indexOf(")");
            if(end<0) {
                break;
            }
            String validUrl = sb.substring(0, end).trim();
            if(validUrl.length()<5) {
                continue;
            }
            if(validUrl.startsWith("//")) {
                validUrl = "https:" + validUrl;
            }else if(!validUrl.startsWith("https:")){
                validUrl = "https://"+validUrl;
            }
            images.add(validUrl);
        }
        return images;
    }
}
